package cn.openread.demo.mq.rabbit;

import com.rabbitmq.client.*;

import java.util.HashMap;
import java.util.Map;

public class Producter {

    public static void main(String args[]) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("rabbitmq-jia24-test.frp.openread.cn");
        factory.setUsername("guest");
        factory.setPort(10062);//注意这里的端口与管理插件的端口不一样
        factory.setPassword("guest");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //声明一个dirent模式的交换机
        channel.exchangeDeclare("exchange_name", BuiltinExchangeType.DIRECT, true);

        //声明一个非持久化自动删除的队列
        channel.queueDeclare("simon-test", false, false, false, null);
        //将绑定到改交换机
        channel.queueBind("simon-test", "exchange_name", "route_key");
        //声明一个消息头部
        Map<String, Object> header = new HashMap<>();

        AMQP.BasicProperties.Builder b = new AMQP.BasicProperties.Builder();
        header.put("charset", "utf-8");
        b.headers(header);
        AMQP.BasicProperties bp = b.build();
        //将消息发出去
        channel.basicPublish("exchange_name", "route_key", false, bp, "test3".getBytes());
    }
}
